package com.kuaifawu.lwnlawyerclient.Activity;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.kuaifawu.lwnlawyerclient.LWNNetwork.LWNNetworkCenter;
import com.kuaifawu.lwnlawyerclient.Lib.ToastView_custom;
import com.kuaifawu.lwnlawyerclient.R;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.umeng.analytics.MobclickAgent;

import org.json.JSONObject;

/**
 * Created by wyk123 on 2015/7/6.
 */
public class LWNReportActivity extends Activity implements View.OnClickListener{
    @ViewInject(R.id.report_back)
    private ImageButton report_back;
    @ViewInject(R.id.save_reportView)
    private ImageButton save_reportView;
    @ViewInject(R.id.edit_report_data)
    private EditText edit_report_data;
    private String questionid="";
    private Activity activity=null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_report);
        ViewUtils.inject(this);
        Intent intent=getIntent();
        Bundle bundle=intent.getExtras();
        if(bundle.containsKey("questionId")){
            questionid=bundle.getString("questionId");
        }
        activity=this;
        inItView();
    }

    private void inItView(){
        report_back.setOnClickListener(this);
        save_reportView.setOnClickListener(this);
        //edit_report_data.setOnClickListener(this);



    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.report_back:
                finish();
                break;
            case R.id.save_reportView:
                String data=edit_report_data.getText().toString();
                   if(data.equals("")||data.length()==0){
                      // Toast.makeText(LWNReportActivity.this,"请输入内容...",Toast.LENGTH_SHORT).show();
                       ToastView_custom toast = new ToastView_custom(LWNReportActivity.this);
                       toast.showCustom(LWNReportActivity.this,"请输入内容...");
                       }else{
                       saveReport(data,questionid,activity);
                        }
          break;
        }

    }

    public static void saveReport(String data,String questionId, final Activity context){
       final ProgressDialog dialog=android.app.ProgressDialog.show(context, "", "提交中，请稍候...");
        LWNNetworkCenter center=LWNNetworkCenter.getInstance();
        RequestParams params= center.getInstance().getReport(data, context, questionId);
        String url=center.LWNBaseUrl_POST + "lawyReport";
        HttpUtils http=center.getUtils();
        http.configDefaultHttpCacheExpiry(0*0);
        http.send(HttpRequest.HttpMethod.POST,url,params,new RequestCallBack<Object>() {
            @Override
            public void onSuccess(ResponseInfo<Object> objectResponseInfo) {

                try {
                    JSONObject jsonObject = new JSONObject(objectResponseInfo.result.toString());
                    String string_code = jsonObject.getString("code");
                    String string_msg = jsonObject.getString("msg");
//                    Toast toast = Toast.makeText(context.getApplicationContext(),
//                            string_msg, Toast.LENGTH_SHORT);
//                    toast.setGravity(Gravity.CENTER, 0, 0);
//                    LinearLayout toastView = (LinearLayout) toast.getView();
//                    ImageView imageCodeProject = new ImageView(context.getApplicationContext());
                    ToastView_custom toast = new ToastView_custom(context);
//                    toast.showCustom(context, "请输入内容");
                    dialog.dismiss();
                    switch (Integer.parseInt(string_code)){
                        case 10000: {
//                            imageCodeProject.setImageResource(R.drawable.success);
//                            toastView.addView(imageCodeProject, 0);
//                            toast.show();
                            toast.showCustom(context, string_msg);
                            context.finish();
                        }
                        break;
                        case 10002:{
//                            imageCodeProject.setImageResource(R.drawable.error);
//                            toastView.addView(imageCodeProject, 0);
//                            toast.show();
                            toast.showCustom(context, "操作失败");
                        }
                        break;
                        case 10003:{


                        }
                        break;


                    }
                }catch (Exception e){

                }


            }

            @Override
            public void onFailure(HttpException e, String s) {
                dialog.dismiss();
            }
        });


    }


    @Override
    protected void onResume() {
        super.onResume();
        MobclickAgent.onPageStart("SplashScreen");
        MobclickAgent.onResume(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd("SplashScreen");
        MobclickAgent.onPause(this);
    }
}
